package com.mkh.st.admin.strategy;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mkh.st.admin.api.entity.SysBusinessEntity;
import com.mkh.st.admin.api.entity.SysCompanyInfo;
import com.mkh.st.admin.api.entity.SysUserRoleScope;
import com.mkh.st.admin.api.vo.DataPermissionVO;
import com.mkh.st.admin.api.vo.ScopeDataDeatil;
import com.mkh.st.admin.api.vo.ScopeDataVO;
import com.mkh.st.admin.api.vo.UserRoleScopeVO;
import com.mkh.st.admin.mapper.SysBusinessEntityMapper;
import com.mkh.st.admin.mapper.SysCompanyInfoMapper;
import com.mkh.st.common.core.constant.CommonConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
public class SiteStrategy extends BaseScopeStrategy{

	@Autowired
	private SysBusinessEntityMapper sysBusinessEntityMapper;

	@Autowired
	private SysCompanyInfoMapper sysCompanyInfoMapper;

	private final Map<Long,String> companyMap=new ConcurrentHashMap<>();

	private final List<Long> ids= Arrays.asList(-1L);


	@Override
	public void setDataPermission(DataPermissionVO vo, SysUserRoleScope sysUserRoleScope, Long tenantId) {
		if(ObjectUtil.isNotNull(sysUserRoleScope)) {
			if (CommonConstants.ONE.equals(sysUserRoleScope.getAllFlag())) {
				vo.setSiteAll(Boolean.TRUE);
				List<SysBusinessEntity> list = sysBusinessEntityMapper.selectList(Wrappers.<SysBusinessEntity>lambdaQuery()
						.eq(SysBusinessEntity::getDelFlag, CommonConstants.NOT_DEL).eq(SysBusinessEntity::getStatus, CommonConstants.ONE)
						.eq(SysBusinessEntity::getTenantId, tenantId).orderByDesc(SysBusinessEntity::getBusinessId));
				if (CollectionUtil.isNotEmpty(list)) {
					List<Long> siteIds = list.stream().map(SysBusinessEntity::getBusinessId).collect(Collectors.toList());
					vo.setSiteList(siteIds);
				}
			} else {
				if(StrUtil.isNotBlank(sysUserRoleScope.getIds())){
					List<Long> allSteIds = Convert.toList(Long.class, StrUtil.split(sysUserRoleScope.getIds(), ','));
					//未删除未禁用查询
					List<SysBusinessEntity> list = sysBusinessEntityMapper.selectList(Wrappers.<SysBusinessEntity>lambdaQuery()
							.eq(SysBusinessEntity::getDelFlag, CommonConstants.NOT_DEL).eq(SysBusinessEntity::getStatus, CommonConstants.ONE)
							.in(SysBusinessEntity::getBusinessId, allSteIds));
					if (CollectionUtil.isNotEmpty(list)) {
						List<Long> siteIds = list.stream().map(SysBusinessEntity::getBusinessId).collect(Collectors.toList());
						vo.setSiteList(siteIds);
					}else {
						vo.setSiteList(ids);
					}
				}else {
					vo.setSiteList(ids);
				}
			}
		}else {
			vo.setSiteList(ids);
		}
	}

	@Override
	public Long setKey() {
		return 2L;
	}

	@Override
	public ScopeDataDeatil getScopeDataList(DataPermissionVO vo, SysUserRoleScope one,Long scopeId) {
		ScopeDataDeatil deatil=new ScopeDataDeatil();
		deatil.setAllFlag(vo.getSiteAll());
		deatil.setScopeId(scopeId);
		List<ScopeDataVO> scopeList =new ArrayList<>();
		if(CollectionUtil.isEmpty(vo.getSiteList())){
			return deatil;
		}
		//当前操作人的子站点权限
		List<SysBusinessEntity> list = sysBusinessEntityMapper.selectBatchIds(vo.getSiteList());
		//被操作账号已有的子站点权限
		boolean flag=false;
		List<Long> exist=new ArrayList<>();
		if(ObjectUtil.isNotNull(one)){
			if(one.getAllFlag().equals(CommonConstants.ONE)){
				flag=true;
			}else if(StrUtil.isNotBlank(one.getIds())){
				exist=Convert.toList(Long.class, StrUtil.split(one.getIds(),','));
			}
		}
		if(CollectionUtil.isNotEmpty(list)){
			for (SysBusinessEntity item:list) {
				ScopeDataVO scopeDataVO=new ScopeDataVO();
				scopeDataVO.setId(item.getBusinessId()).setCode(item.getBusinessCode()).setName(item.getBusinessName());
				if(flag || exist.stream().filter(v ->v.equals(item.getBusinessId())).findFirst().isPresent()){
					scopeDataVO.setChecked(Boolean.TRUE);
				}
				if(companyMap.containsKey(item.getCompanyId())){
					scopeDataVO.setParentName(companyMap.get(item.getCompanyId()));
				}else {
					SysCompanyInfo sysCompanyInfo = sysCompanyInfoMapper.selectById(item.getCompanyId());
					scopeDataVO.setParentName(sysCompanyInfo.getCompanyName());
					companyMap.put(item.getCompanyId(),sysCompanyInfo.getCompanyName());
				}
				scopeList.add(scopeDataVO);
			}
			deatil.setScopeList(scopeList);
		}
		return deatil;
	}

	@Override
	public void getUserDataPermission(DataPermissionVO vo, DataPermissionVO item) {
		if(CollectionUtil.isNotEmpty(item.getSiteList())){
			if(vo.getSiteAll()){
				return;
			}else if(item.getSiteAll()){
				vo.setSiteAll(item.getSiteAll());
				vo.setSiteList(item.getSiteList());
			}else {
				if(CollectionUtil.isNotEmpty(vo.getSiteList())){
					List<Long> collect = Stream.of(vo.getSiteList(),item.getSiteList()).flatMap(Collection::stream).distinct().collect(Collectors.toList());
					vo.setSiteList(collect);
				}else {
					vo.setSiteList(item.getSiteList());
				}
			}
		}
	}

	@Override
	public List<Long> getList(Long tenantId) {
		List<SysBusinessEntity> list = sysBusinessEntityMapper.selectList(Wrappers.<SysBusinessEntity>lambdaQuery()
				.eq(SysBusinessEntity::getDelFlag, CommonConstants.NOT_DEL).eq(SysBusinessEntity::getStatus, CommonConstants.ONE)
				.eq(SysBusinessEntity::getTenantId, tenantId).orderByDesc(SysBusinessEntity::getBusinessId));
		if (CollectionUtil.isNotEmpty(list)) {
			List<Long> siteIds = list.stream().map(SysBusinessEntity::getBusinessId).collect(Collectors.toList());
			return siteIds;
		}
		return null;
	}

	@Override
	public void getAllList(UserRoleScopeVO userRoleScopeVO, SysUserRoleScope scope, Long tenantId) {
		if(CommonConstants.ZERO.equals(scope.getAllFlag())&& StrUtil.isBlank(scope.getIds())){
			return;
		}
		Boolean all=false;
		if(CommonConstants.ONE.equals(scope.getAllFlag())){
			all=true;
		}
		//未删除未禁用查询
		List<SysBusinessEntity> list = sysBusinessEntityMapper.selectList(Wrappers.<SysBusinessEntity>lambdaQuery()
				.eq(SysBusinessEntity::getDelFlag, CommonConstants.NOT_DEL)
				.eq(SysBusinessEntity::getStatus, CommonConstants.ONE)
				.eq(SysBusinessEntity::getTenantId, tenantId)
				.in(!all,SysBusinessEntity::getBusinessId, Convert.toList(Long.class, StrUtil.split(scope.getIds(), ','))));
		if (CollectionUtil.isNotEmpty(list)) {
			List<ScopeDataVO> scopeList =new ArrayList<>();
			for (SysBusinessEntity item:list) {
				ScopeDataVO scopeDataVO=new ScopeDataVO();
				scopeDataVO.setId(item.getBusinessId()).setCode(item.getBusinessCode()).setName(item.getBusinessName());
				if(companyMap.containsKey(item.getCompanyId())){
					scopeDataVO.setParentName(companyMap.get(item.getCompanyId()));
				}else {
					SysCompanyInfo sysCompanyInfo = sysCompanyInfoMapper.selectById(item.getCompanyId());
					scopeDataVO.setParentName(sysCompanyInfo.getCompanyName());
					companyMap.put(item.getCompanyId(),sysCompanyInfo.getCompanyName());
				}
				scopeList.add(scopeDataVO);
			}
			userRoleScopeVO.setScopeList(scopeList);
		}
	}

	@Override
	public Boolean checkPermission(SysUserRoleScope one, String name, Long tenantId) {
		return null;
	}
}
